package com.defysoft.waukids.main;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.json.JSONObject;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.defysoft.waukids.BaseActivity;
import com.defysoft.waukids.Data_Sharedpreferences;
import com.defysoft.waukids.PublicSharedpreferences;
import com.defysoft.waukids.R;

public class Main_Admin_Notice extends BaseActivity {
	String sub = "";
	TextView tv;

	long now = System.currentTimeMillis();
	Date date = new Date(now);
	SimpleDateFormat today = new SimpleDateFormat("yyyy-MM-dd");
	PublicSharedpreferences psf;
	SharedPreferences.Editor editor;
	public Data_Sharedpreferences.SF_Adnotice adnotice;
	private final int FAIL_CONNECT = 3;
	private final int ADNOT_SUCCESS = 2;
	private final int ADNOT_FAIL = 4;

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.main_admin_notice);
		startNetwork(0);
		psf = new PublicSharedpreferences(this);
		psf.setAdnotice();
		adnotice =psf.getAdnotice();
		
		((LinearLayout) findViewById(R.id.AD_NOT_ONEDAY))
				.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						((ImageView) findViewById(R.id.AD_NOT_CHECK)).setImageResource(R.drawable.form_check_on);
						editor = psf.getAdnotice_edit();
						editor.putString(PublicSharedpreferences.SF_Adnotice_Key.DATE, today.format(date));
						editor.commit();
						
						finish();
					}
				});

		((ImageView) findViewById(R.id.AD_NOT_CLOSE))
				.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
							finish();
					}
				});
	}

	@Override
	protected void handlingMessage(Message _message) {
		// TODO Auto-generated method stub
		super.handlingMessage(_message);
		
		switch (_message.what) {
		case ADNOT_SUCCESS:
			if (firstThread != null) {
				firstThread.interrupt();
			}
			((TextView)findViewById(R.id.AD_NOT_TEXT)).setText(sub);
			break;
		case ADNOT_FAIL:
			if (firstThread != null) {
				firstThread.interrupt();
			}
			break;
		case FAIL_CONNECT:
			if (firstThread != null) {
				firstThread.interrupt();
			}
			break;
		}
		
	}
	
	@Override
	public void startThread(int choice) {
		super.startThread(choice);
		final int cho=choice;
	firstThread = new Thread(new Runnable() {
		
		@Override
		public void run() {
			switch (cho) {
			case 0:
				getAdnoticeData();
				break;

			default:
				break;
			}			
		}
		
	});
		firstThread.start();
		
	}

	public void getAdnoticeData() {

		try {
			JSONObject jsonObject = new JSONObject(publicHttpCallback(HTTP_URL,
					"code=" + CODE_ADNOTICE));
			if (jsonObject.getBoolean("success")) {
				sub = jsonObject.getString("sub");

				onNetworkCallback(ADNOT_SUCCESS, "");
			} else {
				onNetworkCallback(ADNOT_FAIL, "");
			}
		} catch (Exception ex) {
			onNetworkCallback(FAIL_CONNECT, ex.toString()
					+ " where getAdnotice()");
		}

	}

}
